package com.ruoyi.project.simple.service.impl;

import java.util.List;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.lang.UUID;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.IdUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.redis.RedisCache;
import com.ruoyi.project.simple.domain.SplOrderDetail;
import com.ruoyi.project.simple.domain.SplOrderDetailReport;
import com.ruoyi.project.simple.domain.vo.SplOrderVO;
import com.ruoyi.project.simple.mapper.SplOrderDetailMapper;
import com.ruoyi.project.simple.service.ISplOrderDetailReportService;
import com.ruoyi.project.simple.service.ISplOrderDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.simple.mapper.SplOrderMapper;
import com.ruoyi.project.simple.domain.SplOrder;
import com.ruoyi.project.simple.service.ISplOrderService;

/**
 * 送检样品订单Service业务层处理
 *
 * @author zhujj
 * @date 2020-01-09
 */
@Service
public class SplOrderServiceImpl implements ISplOrderService {
    @Autowired
    private SplOrderMapper splOrderMapper;

    @Autowired
    private ISplOrderDetailService splOrderDetailService;

    @Autowired
    private ISplOrderDetailReportService splOrderDetailReportService;


    @Autowired
    private RedisCache redisCache;
    /**
     * 查询送检样品订单
     *
     * @param orderId 送检样品订单ID
     * @return 送检样品订单
     */
    @Override
    public SplOrderVO selectSplOrderById(String orderId) {
        return splOrderMapper.selectSplOrderById(orderId);
    }

    /**
     * 查询送检样品订单列表
     *
     * @param splOrder 送检样品订单
     * @return 送检样品订单
     */
    @Override
    public List<SplOrder> selectSplOrderList(SplOrder splOrder) {
        return splOrderMapper.selectSplOrderList(splOrder);
    }

    /**
     * 新增送检样品订单
     *
     * @param splOrder 送检样品订单
     * @return 结果
     */
    @Override
    public SplOrderVO insertSplOrder(SplOrderVO splOrder) {
        splOrder.setOrderId(IdUtils.fastSimpleUUID());
        splOrder.setOrderNumber(redisCache.generate("orderNumber"));
        splOrder.setUpdateBy(SecurityUtils.getUsername());
        splOrder.setCreateBy(SecurityUtils.getUsername());
        splOrder.setCreateTime(DateUtils.getNowDate());
        splOrder.setUpdateTime(DateUtils.getNowDate());
        splOrderMapper.insertSplOrder(splOrder);
        List<SplOrderDetail> orderDetail = splOrder.getOrderDetail();
        for (SplOrderDetail splOrderDetail : orderDetail) {
            splOrderDetail.setOrderId(splOrder.getOrderId());
            splOrderDetail.setOrderNumber(redisCache.generate("simpleTypeNumber"));

            splOrderDetailService.insertSplOrderDetail(splOrderDetail);
            SplOrderDetailReport splOrderDetailReport = new SplOrderDetailReport();
            splOrderDetailReport.setOrderDetailId(splOrderDetail.getOrderDetailId());
            splOrderDetailReportService.insertSplOrderDetailReport(splOrderDetailReport);
        }
        return splOrder;
    }

    /**
     * 修改送检样品订单
     *
     * @param splOrder 送检样品订单
     * @return 结果
     */
    @Override
    public int updateSplOrder(SplOrder splOrder) {
        splOrder.setUpdateBy(SecurityUtils.getUsername());
        splOrder.setUpdateTime(DateUtils.getNowDate());
        return splOrderMapper.updateSplOrder(splOrder);
    }
    /**
     * 修改送检样品订单
     *
     * @param splOrder 送检样品订单
     * @return 结果
     */
    @Override
    public int updateSplOrderPayed(SplOrder splOrder) {
        splOrder.setUpdateBy("weixin");
        splOrder.setUpdateTime(DateUtils.getNowDate());
        return splOrderMapper.updateSplOrder(splOrder);
    }
    @Override
    public int acceptOrder(SplOrder splOrder) {
        splOrder.setUpdateBy(SecurityUtils.getUsername());
        splOrder.setUpdateTime(DateUtils.getNowDate());
        if ("5".equals(splOrder.getDelFlag())) {
            SplOrderDetail splOrderDetail = new SplOrderDetail();
            splOrderDetail.setOrderId(splOrder.getOrderId());
            splOrderDetail.setState(20);
            splOrderDetailService.updateSplOrderDetailByOrderId(splOrderDetail);
        }
        return splOrderMapper.updateSplOrder(splOrder);
    }

    /**
     * 批量删除送检样品订单
     *
     * @param orderIds 需要删除的送检样品订单ID
     * @return 结果
     */
    @Override
    public int deleteSplOrderByIds(String[] orderIds) {
        return splOrderMapper.deleteSplOrderByIds(orderIds);
    }

    /**
     * 删除送检样品订单信息
     *
     * @param orderId 送检样品订单ID
     * @return 结果
     */
    @Override
    public int deleteSplOrderById(String orderId) {
        return splOrderMapper.deleteSplOrderById(orderId);
    }


}
